package threadlocal;

public class T1 {

    //创建ThreadLocal对象, 做成public static.
    public static ThreadLocal<Object> threadLocal1 = new ThreadLocal<>();
    public static ThreadLocal<Object> threadLocal2 = new ThreadLocal<>();

    //Task 是线程类 -> 内部类/线程
    public static class Task implements Runnable{
        @Override
        public void run() {
            Dog dog = new Dog();
            Pig pig = new Pig();
            System.out.println("Task 放入了 dog= " + dog);
            //给threadLocal1 对象放入set dog
            threadLocal1.set(dog);
            //threadLocal1.set(pig);给同一个threadlocal传值，值将会被替换
            threadLocal2.set(pig);//这个数据就会threadLocal2关联，并且都被当前Thread管理
            System.out.println("Task 在run 方法中 线程=" + Thread.currentThread().getName());
            new T1Service().update();
        }
    }

    public static void main(String[] args) {
        new Thread(new Task()).start();//主线程启动一个新的线程,注意不是主线程
    }
}
